/**
 * @Title: ReserveController.java
 * @Package com.qding.park.api.web.service.controller
 * @Description: TODO
 * Copyright: Copyright (c) 2011 
 * Company:千丁互联
 * 
 * @author Comsys-Administrator
 * @date 2016年8月16日 下午2:48:09
 * @version V1.0
 */

package com.qding.park.manager.rest.controller;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.adapter.AdapterException;
import com.qding.park.common.core.exceptions.business.manager.service.ServiceCoreException;
import com.qding.park.common.core.exceptions.business.validator.RchlValidatorException;
import com.qding.park.common.core.exceptions.system.SystemUnKnown;
import com.qding.park.common.core.validate.RchlValidateBean;
import com.qding.park.common.core.validate.RchlValidateTools;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.common.rest.domain.AnswerMsg;
import com.qding.park.manager.common.rest.domain.AnswerMsgFactoryImpl;
import com.qding.park.manager.common.rest.enums.ResponseCodeEnum;
import com.qding.park.manager.facade.core.service.DemoFacade;
import com.qding.park.manager.facade.core.service.common.ReserveCommonFacade;
import com.qding.park.manager.rest.conf.SystemConfig;

/**
 * @ClassName: ReserveController
 * @Description: 车位预定Controller
 * @author Comsys-Administrator
 * @date 2016年8月16日 下午2:48:09
 *
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/reserve")
public class ReserveController extends WebBaseController {
	private static final Log _log = LogFactory.getLog(ReserveController.class);

//	/**
//	 * @Field @reserveFacade : TODO(预定服务)
//	 */
//	@Autowired
//	private ReserveFacade reserveFacade;
	
	@Autowired
	private SystemConfig systemConfig;
	
	@Autowired
	private DemoFacade demoFacade;
	
	@Autowired
	private  ReserveCommonFacade   reserveCommonFacade;

	/**
	  * createReserveOrder(创建预定订单)
	  *
	  * @Title: createReserveOrder
	  * @Description: TODO
	  * @param @param carNum  车牌号
	  * @param @param reserveStartTime 预定时间
	  * @param @param userId  北京用户id
	  * @param @param parkId  停车场id
	  * @param @return    设定文件
	  * @return AnswerMsg    返回类型
	  * @throws
	  */
	@RequestMapping("/createReserveOrder")
	public AnswerMsg createReserveOrder(String carNum, String reserveStartTime, String userId, String parkId) {
		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("carNum", carNum);
			vb.getRequiredValidate().put("reserveStartTime", reserveStartTime);
			vb.getRequiredValidate().put("userId", userId);
			vb.getRequiredValidate().put("parkId", parkId);
			
			vb.getIntValidate().put("parkId", parkId);
			vb.getLongValidate().put("reserveStartTime", reserveStartTime);
			RchlValidateTools.validate(vb);
			/**************/
			ParkDataResult parkDataResult =null;
			if(isDemoRequest(userId)){
				//演示请求
				String project=systemConfig.projectByParkId(Integer.valueOf(parkId));
				parkDataResult=demoFacade.createReserveOrder(userId,Integer.valueOf(parkId),carNum,
						new Date(Long.valueOf(reserveStartTime) * 1000),project);
			}else{
				parkDataResult = reserveCommonFacade.createReserveOrder(userId,Integer.valueOf(parkId),carNum,
						new Date(Long.valueOf(reserveStartTime) * 1000));
			}
			
			
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {

			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		}
		catch (AdapterException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}
		catch (BaseException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (Exception e) {
			SystemUnKnown ex = new SystemUnKnown(e, ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name,
					"未知Exception", "userId=" + userId));
			exceptionHandle.handle(ex);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}
	}

//	/**
//	 * @Description (车位预定)
//	 * @param carNum
//	 *            车牌
//	 * @param reserveStartTime
//	 *            预定进场时间
//	 * @param userId
//	 *            有户id
//	 * @param parkId
//	 *            车场id
//	 * @param depositNum
//	 *            押金单号
//	 * @return json
//	 */
//	@RequestMapping("/parkSpace")
//	public AnswerMsg parkSpace(String carNum, String reserveStartTime, String userId, String parkId, String depositNum,
//			String depositAmount) {
//		try {
//
//			/****** 验证字段 ********/
//			RchlValidateBean vb = new RchlValidateBean();
//			vb.getRequiredValidate().put("carNum", carNum);
//			vb.getRequiredValidate().put("reserveStartTime", reserveStartTime);
//			vb.getRequiredValidate().put("userId", userId);
//			vb.getRequiredValidate().put("parkId", parkId);
//			vb.getRequiredValidate().put("depositNum", depositNum);
//			vb.getRequiredValidate().put("depositAmount", depositAmount);
//
//			vb.getIntValidate().put("parkId", parkId);
//			vb.getIntValidate().put("depositAmount", depositAmount);
//			vb.getLongValidate().put("reserveStartTime", reserveStartTime);
//			RchlValidateTools.validate(vb);
//			/**************/
//
//			// 预定车位
//			ParkDataResult parkDataResult = reserveFacade.reserveParkSpace(carNum,
//					new Date(Long.valueOf(reserveStartTime) * 1000), userId, Integer.valueOf(parkId), depositNum,
//					Integer.valueOf(depositAmount));
//			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
//			return answerMsg;
//		} catch (RchlValidatorException e) {
//
//			exceptionHandle.handle(e);
//			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
//					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
//			return answerMsg;
//		} catch (BaseException e) {
//			exceptionHandle.handle(e);
//			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
//					e.getExceptionMessage().getSimpleText());
//			return answerMsg;
//		} catch (Exception e) {
//			SystemUnKnown ex = new SystemUnKnown(e, ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name,
//					"未知Exception", "userId=" + userId));
//			exceptionHandle.handle(ex);
//			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
//			return answerMsg;
//		}
//	}
	
//	/**
//	 * @Description (车位预定)
//	 * @param carNum
//	 *            车牌
//	 * @param reserveStartTime
//	 *            预定进场时间
//	 * @param userId
//	 *            有户id
//	 * @param parkId
//	 *            车场id
//	 * @param depositNum
//	 *            押金单号
//	 * @return json
//	 */
//	@RequestMapping("/parkSpace")
//	public AnswerMsg parkSpace(String reserveId) {
//		try {
//			/****** 验证字段 ********/
//			RchlValidateBean vb = new RchlValidateBean();
//			vb.getRequiredValidate().put("reserveId", reserveId);
//			RchlValidateTools.validate(vb);
//			/**************/
//
//			// 预定车位
//			ParkDataResult parkDataResult = reserveFacade.reserveParkSpace(reserveId);
//			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
//			return answerMsg;
//		} catch (RchlValidatorException e) {
//
//			exceptionHandle.handle(e);
//			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
//					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
//			return answerMsg;
//		} catch (BaseException e) {
//			exceptionHandle.handle(e);
//			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
//					e.getExceptionMessage().getSimpleText());
//			return answerMsg;
//		} catch (Exception e) {
//			SystemUnKnown ex = new SystemUnKnown(e, ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name,
//					"未知Exception", "reserveId=" + reserveId));
//			exceptionHandle.handle(ex);
//			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
//			return answerMsg;
//		}
//	}
//	@RequestMapping("/parkSpace")
//	public AnswerMsg parkSpace(String carNum, String reserveStartTime, String userId, String parkId, String depositNum,
//			String depositAmount) {
//		try {
//
//			/****** 验证字段 ********/
//			RchlValidateBean vb = new RchlValidateBean();
//			vb.getRequiredValidate().put("carNum", carNum);
//			vb.getRequiredValidate().put("reserveStartTime", reserveStartTime);
//			vb.getRequiredValidate().put("userId", userId);
//			vb.getRequiredValidate().put("parkId", parkId);
//			vb.getRequiredValidate().put("depositNum", depositNum);
//			vb.getRequiredValidate().put("depositAmount", depositAmount);
//
//			vb.getIntValidate().put("parkId", parkId);
//			vb.getIntValidate().put("depositAmount", depositAmount);
//			vb.getLongValidate().put("reserveStartTime", reserveStartTime);
//			RchlValidateTools.validate(vb);
//			/**************/
//
//			// 预定车位
//			ParkDataResult parkDataResult = reserveFacade.reserveParkSpace(carNum,
//					new Date(Long.valueOf(reserveStartTime) * 1000), userId, Integer.valueOf(parkId), depositNum,
//					Integer.valueOf(depositAmount));
//			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
//			return answerMsg;
//		} catch (RchlValidatorException e) {
//
//			exceptionHandle.handle(e);
//			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
//					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
//			return answerMsg;
//		} catch (BaseException e) {
//			exceptionHandle.handle(e);
//			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
//					e.getExceptionMessage().getSimpleText());
//			return answerMsg;
//		} catch (Exception e) {
//			SystemUnKnown ex = new SystemUnKnown(e, ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name,
//					"未知Exception", "userId=" + userId));
//			exceptionHandle.handle(ex);
//			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
//			return answerMsg;
//		}
//	}

	/**
	 * @param userId
	 * @return 查询用户名下预定列表 引方法暂不实现
	 */
	@RequestMapping("/bookingList")
	public AnswerMsg bookingList(String userId) {
			
		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("userId", userId);
			RchlValidateTools.validate(vb);
			/**************/
			
			ParkDataResult parkDataResult =null;
			if(this.isDemoRequest(userId)){
				//演示请求
				parkDataResult=this.demoFacade.queryReservesByBjuserId(userId);
			}else{
				// 预定车位
				parkDataResult = reserveCommonFacade.queryReservesByBjuserId(userId);
			}
			
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (ServiceCoreException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getName());
			return answerMsg;
		} catch (RchlValidatorException e) {

			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		} catch (BaseException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (Exception e) {
			SystemUnKnown ex = new SystemUnKnown(e, ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name,
					"未知Exception", "userId=" + userId));
			exceptionHandle.handle(ex);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}
	}

	/**
	 * @param reserveId
	 * @return 根据订单号查询预定详情
	 */
	@RequestMapping("/bookInfo")
	public AnswerMsg bookInfo(String reserveId) {

		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("reserveId", reserveId);

			// vb.getIntValidate().put("reserveId", reserveId);
			RchlValidateTools.validate(vb);
			/**************/

			// 预定车位
			ParkDataResult parkDataResult = reserveCommonFacade.queryReservesInfoById(reserveId);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {

			e.handle();
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		} catch (BaseException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (Exception e) {
			SystemUnKnown ex = new SystemUnKnown(e, ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name,
					"未知Exception", "reserveId=" + reserveId));
			exceptionHandle.handle(ex);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}
	}

	/**
	 * @param reserveId
	 * @return 取消预定
	 */
	@RequestMapping("/cancelReserve")
	public AnswerMsg cancelReserve(String reserveId) {
		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("reserveId", reserveId);
			RchlValidateTools.validate(vb);
			/**************/

			// 预定车位
			ParkDataResult parkDataResult =null;
			if(this.isDemoRequestByOrder(reserveId)){
				//演示请求
				return AnswerMsgFactoryImpl.createAnswerMsg(ParkDataResult.createEmpty().getData());
			}else{
				parkDataResult = reserveCommonFacade.cancelResecancelById(reserveId);
			}
			
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {

			e.handle();
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		} 
		catch (AdapterException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}
		catch (BaseException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (Exception e) {
			SystemUnKnown ex = new SystemUnKnown(e, ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name,
					"未知Exception", "reserveId=" + reserveId));
			exceptionHandle.handle(ex);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}
	}
}
